<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        position: absolute;
        left: 0px;
        top: 0px;
        background-color: pink;
      }
    </style>
  </head>

  <body>
    <div class="box"></div>
    <script>
      //  定义一个运动函数
      function move(el, direct, distance) {
        return new Promise((resolve) => {
          el.style.transition = "all 1s"; //设置过渡
          el.style[direct] = distance; //设置运动的距离
          el.addEventListener("transitionend", resolve); //监听过渡结束
        });
      }
      // 获取到运动的对象
      let boxEle = document.querySelector(".box");
      boxEle.onclick = function () {
        //点击元素执行事件
        // 去调用运动函数
        // 注意direct和distance要加引号和单位
        move(boxEle, "left", "300px")
          .then(() => {
            return move(boxEle, "top", "300px");
          })
          .then(() => {
            return move(boxEle, "left", "0px");
          })
          .then(() => {
            return move(boxEle, "top", "0px");
          })
          .then(() => {
            console.log("运动结束啦");
          });
      };
    </script>
  </body>
</html>
